package com.tsxy.mall.mapper;

import com.tsxy.mall.entity.BookSort;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.lang.reflect.Method;
import java.util.List;

@Repository
@Mapper
public interface BookSortMapper {

    

    @Select({"select * from book_sort where parent is null "})
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "parent",property = "parent"),
            @Result(column = "status",property = "status"),
            @Result(column = "id",property = "children",
                many = @Many(select = "getSortByParent")),
    })
    List<BookSort> getAllSort();

    @Select({"select * from book_sort where parent = #{parent} "})
    @Results({
         @Result(column = "id",property = "id"),
         @Result(column = "name",property = "name"),
    })
    List<BookSort> getSortByParent(Integer parent);

    @Select({"select id from book_sort where parent = #{id}"})
    List<Integer> selectChildren(Integer id);

    @Select({
            "select",
            "*",
            "from book_sort",
            "where id = #{id}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType= JdbcType.INTEGER, id=true),
            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
            @Result(column="parent", property="parent", jdbcType=JdbcType.INTEGER),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
            @Result(column = "parent" ,property = "parentSort",
                    one = @One(select ="selectByPrimaryKey") )
    })
    BookSort selectByPrimaryKey(@Param("id") Integer id);
}
